/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cc.angis.xiugallery.image.util;

import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Thumbnails;
import android.util.Log;
import cc.angis.xiugallery.BuildConfig;
import cc.angis.xiugallery.db.FavouriteImagesDBHelper;
import cc.angis.xiugallery.db.TableConstants;
import cc.angis.xiugallery.provider.FavouriteGalleryProvider;
import cc.angis.xiugallery.provider.SystemGalleryProvider;

/**
 * A simple subclass of {@link ImageResizer} that fetches and resizes images fetched from a URL.
 */
public class ThumbnailImageLoader extends ImageResizer {
    private static final String TAG = "ImageLoader";
    private int type;
    
    /**
     * Initialize providing a target image width and height for the processing images.
     *
     * @param context
     * @param imageWidth
     * @param imageHeight
     */
    public ThumbnailImageLoader(Context context, int imageWidth, int imageHeight, int type) {
        super(context, imageWidth, imageHeight);
        this.type = type;
    }

//    /**
//     * Initialize providing a target image width and height for the processing images.
//     *
//     * @param context
//     * @param imageWidth
//     * @param imageHeight
//     */
//    public ThumbnailImageLoader(Context context, int imageWidth, int imageHeight) {
//        super(context, imageWidth, imageHeight);
//    }

//    /**
//     * Initialize providing a single target image size (used for both width and height);
//     *
//     * @param context
//     * @param imageSize
//     */
//    public ThumbnailImageLoader(Context context, int imageSize) {
//        super(context, imageSize);
//    }
    
    /**
     * Initialize providing a single target image size (used for both width and height);
     *
     * @param context
     * @param imageSize
     */
    public ThumbnailImageLoader(Context context, int imageSize, int type) {
        super(context, imageSize);
        this.type = type;
    }

    /**
     * The main process method, which will be called by the ImageWorker in the AsyncTask background
     * thread.
     *
     * @param data The data to load the bitmap, in this case, a regular http URL
     * @return The downloaded and resized bitmap
     */
    private Bitmap processBitmap(String data) {
        if (BuildConfig.DEBUG) {
            Log.d(TAG, "processBitmap - " + data);
        }

        return initImages(mContext, data);
    }

    @Override
    protected Bitmap processBitmap(Object data) {
        return processBitmap(String.valueOf(data));
    }

 
    private Bitmap initImages(Context context, String matchkey) {
        if (BuildConfig.DEBUG) {
            Log.d(TAG, "loadBitmap - loading - " + matchkey);
        }
        try{
//        	if(imageId == -1){
//        		FavouriteImagesDBHelper fiDBHelper = new FavouriteImagesDBHelper(context, TableConstants.TABLE_FAVOURITE_IMAGES);
//        		imageId = fiDBHelper.getLastImageId(Integer.parseInt(matchkey));
//        		fiDBHelper.closeDB();
//        	}
//			    final BitmapFactory.Options options = new BitmapFactory.Options();
//			    options.outWidth = mImageWidth;
//			    options.outHeight = mImageHeight;
//		        options.inJustDecodeBounds = true;
		        Bitmap bitmap =  MediaStore.Images.Thumbnails.getThumbnail(mContext.getContentResolver(), Long.parseLong(matchkey), type, null);
		        if (type != Thumbnails.MINI_KIND)
		        	return bitmap;
		        Bitmap result = ThumbnailUtils.extractThumbnail(bitmap, mImageWidth, mImageHeight*80/110);
		        if (!bitmap.isRecycled()){
		        	bitmap.recycle();
		        	bitmap = null;
		        }
		        return result;
		        // Calculate inSampleSize
//		        options.inSampleSize = calculateInSampleSize(options, mImageWidth, mImageHeight);

		        // Decode bitmap with inSampleSize set
//		        options.inJustDecodeBounds = false;
		        
		       // return MediaStore.Images.Thumbnails.getThumbnail(mContext.getContentResolver(), Long.parseLong(matchkey), MediaStore.Images.Thumbnails.MINI_KIND, options);
			    
        }catch(Exception e){
        	return null;
        }
	}
}
